package com.dtjd.playcoinmonkey.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.dtjd.playcoinmonkey.ChildClick;
import com.dtjd.playcoinmonkey.R;
import com.dtjd.playcoinmonkey.bean.HomeListBean;
import com.dtjd.playcoinmonkey.databinding.ItemHomeAdsBinding;
import com.dtjd.playcoinmonkey.databinding.ItemHomeBinding;

import java.util.List;
public class HomeListAdapter extends ArrayAdapter<HomeListBean> {
    private final Activity context;
    private final ChildClick delete, refreshTime,cancelCollection;
    public HomeListAdapter(Activity context, List<HomeListBean> objects, ChildClick delete, ChildClick refreshTime, ChildClick cancelCollection){
        super(context, R.layout.item_home,objects);
        this.context=context;
        this.delete=delete;
        this.refreshTime = refreshTime;
        this.cancelCollection=cancelCollection;
    }
    @SuppressLint({"SetTextI18n", "StringFormatMatches"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        HomeListBean bean = getItem(position);
        Object ad=getItem(position).ad;
        if (ad!=null){
            ItemHomeAdsBinding itemHomeAdsBinding=ItemHomeAdsBinding.inflate(context.getLayoutInflater());
            itemHomeAdsBinding.shimmer.startShimmerAnimation();
            TTNativeExpressAd advertisement;
            if (ad instanceof TTNativeExpressAd){
                advertisement = (TTNativeExpressAd)ad;
                advertisement.render();
                itemHomeAdsBinding.itemHomeAds.removeAllViews();
                View adView=advertisement.getExpressAdView();
                if (adView!=null){
                    ViewGroup p = (ViewGroup) adView.getParent();
                    if (p==null){
                        itemHomeAdsBinding.itemHomeAds.addView(adView);
                        itemHomeAdsBinding.shimmer.stopShimmerAnimation();
                        itemHomeAdsBinding.itemHomeGujia.setVisibility(View.GONE);
                    }else{
                        p.removeView(adView);
                    }
                }
                return itemHomeAdsBinding.getRoot();
//                return getVideoView(convertView,parent,advertisement);
            }
            if (String.valueOf(ad).equals("have")){
                itemHomeAdsBinding.itemHomeAds.removeAllViews();
                itemHomeAdsBinding.shimmer.startShimmerAnimation();
                TTAdNative mTTAdNative = TTAdSdk.getAdManager().createAdNative(context);
                AdSlot adSlot = new AdSlot.Builder().setCodeId("946910815").setSupportDeepLink(true).setAdCount(1).setExpressViewAcceptedSize(375,0).build();
                mTTAdNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
                    @Override
                    public void onError(int code, String message) { }
                    @Override
                    public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                        TTNativeExpressAd ad = ads.get(0);
                        ad.render();
                        View adView=ad.getExpressAdView();
                        if (adView!=null){
                            ViewGroup p = (ViewGroup) adView.getParent();
                            if (p==null){
                                itemHomeAdsBinding.itemHomeAds.addView(adView);
                                itemHomeAdsBinding.shimmer.stopShimmerAnimation();
                                itemHomeAdsBinding.itemHomeGujia.setVisibility(View.GONE);
                            }else{
                                p.removeView(adView);
                            }
                        }
                        ad.setDislikeCallback(context, new TTAdDislike.DislikeInteractionCallback() {
                            @Override
                            public void onShow() {}
                            @Override
                            public void onSelected(int i, String s, boolean b) {
                                remove(getItem(position));
                                notifyDataSetChanged();
                            }
                            @Override
                            public void onCancel() { }
                        });
                    }
                });
            }
            return itemHomeAdsBinding.getRoot();
        }else {
            @SuppressLint("ViewHolder") ItemHomeBinding itemHomeBinding = ItemHomeBinding.inflate(context.getLayoutInflater());
            if ( bean.img.equals("")){
                itemHomeBinding.itemHomeImg.setVisibility(View.GONE);
            }else {
                Glide.with(context).load(bean.img).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(itemHomeBinding.itemHomeImg);
                itemHomeBinding.itemHomeImg.setVisibility(View.VISIBLE);
            }
            if (bean.type==0){//HOME
                itemHomeBinding.itemHomeOperate.setVisibility(View.GONE);
                itemHomeBinding.itemHomeCancelCollect.setVisibility(View.GONE);
                itemHomeBinding.itemHomeThaw.setVisibility(View.GONE);
            }
            if (bean.type==1){//
                itemHomeBinding.itemHomeOperate.setVisibility(View.VISIBLE);
                itemHomeBinding.itemHomeCancelCollect.setVisibility(View.GONE);
                itemHomeBinding.itemHomeThaw.setVisibility(View.GONE);
            }
            if (bean.type==2){//COLLECT
                itemHomeBinding.itemHomeOperate.setVisibility(View.GONE);
                itemHomeBinding.itemHomeCancelCollect.setVisibility(View.VISIBLE);
                itemHomeBinding.itemHomeThaw.setVisibility(View.GONE);
            }
            if (bean.type==3){//MY POST
                itemHomeBinding.itemHomeOperate.setVisibility(View.VISIBLE);
                itemHomeBinding.itemHomeRefreshTime.setVisibility(View.GONE);
                itemHomeBinding.itemHomeCancelCollect.setVisibility(View.GONE);
                itemHomeBinding.itemHomeThaw.setVisibility(View.GONE);
            }
            if (bean.type==4){
                itemHomeBinding.itemHomeOperate.setVisibility(View.GONE);
                itemHomeBinding.itemHomeRefreshTime.setVisibility(View.GONE);
                itemHomeBinding.itemHomeCancelCollect.setVisibility(View.GONE);
                itemHomeBinding.itemHomeThaw.setVisibility(View.VISIBLE);
            }
            Glide.with(context).load(bean.wb_url).placeholder(R.drawable.loading).apply(RequestOptions.bitmapTransform(new CircleCrop())).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(itemHomeBinding.itemHomeIcon);
            itemHomeBinding.itemHomeTitle.setText(bean.bp_title);
            itemHomeBinding.itemHomeName.setText(bean.wb_nickname);
            itemHomeBinding.itemHomeContent.setText(bean.bp_content.replace("\n","  "));
            itemHomeBinding.itemHomeTime.setText(bean.time);
            itemHomeBinding.itemHomeTime.setText(bean.time);
            itemHomeBinding.itemHomeView.setText(String.format(context.getResources().getString(R.string.viewsNum),"0"));
            itemHomeBinding.itemHomeView.setText(String.format(context.getResources().getString(R.string.viewsNum),bean.bp_click_number));
            itemHomeBinding.itemHomeRefreshTime.setOnClickListener(view -> refreshTime.click(view,position));
            itemHomeBinding.itemHomeDelete.setOnClickListener(view -> delete.click(view,position));
            itemHomeBinding.itemHomeCancelCollect.setOnClickListener(view -> cancelCollection.click(view,position));
            return itemHomeBinding.getRoot();
        }
    }
    private static class AdViewHolder {
        FrameLayout videoView;
    }
    private View getVideoView(View convertView, ViewGroup parent, @NonNull final TTNativeExpressAd ad) {
        final AdViewHolder adViewHolder;
        try {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.listitem_ad_native_express, parent, false);
                adViewHolder = new AdViewHolder();
                adViewHolder.videoView = (FrameLayout) convertView.findViewById(R.id.iv_listitem_express);
                convertView.setTag(adViewHolder);
            } else {
                adViewHolder = (AdViewHolder) convertView.getTag();
            }
            if (adViewHolder.videoView != null) {
                View video = ad.getExpressAdView();
                if (video != null) {
                    if (video.getParent() == null) {
                        adViewHolder.videoView.removeAllViews();
                        adViewHolder.videoView.addView(video);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }
}